//
//  NewFeatureViewController.swift
//  sina
//
//  Created by 江先铨 on 15/11/13.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

 private let imageCount = 4

class NewFeatureViewController: UICollectionViewController {
    
  
    
    init() {
    
    let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
  
        self.collectionView!.registerClass(NewFeatCell.self, forCellWithReuseIdentifier: reuseIdentifier)
      
        
        
    }



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatCell
    
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.blueColor()
        cell.index = indexPath.item
    
        return cell
    }
    
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        
        if page == imageCount - 1 { //判断是不是最后一页
        
            if let cell = collectionView?.visibleCells().last as? NewFeatCell { //最后的一个可见的cell 是一个NewFeatCell
            
            cell.startAnimation()
            }
        }
    }

    
class  NewFeatCell: UICollectionViewCell {

        var index: Int = 0 {
        
            didSet {
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            }
        }
    
  @objc  private func start() { //这个是oc 的方法 所以需要用objc 作为前缀
    
     //  print("开始体验")
    
    NSNotificationCenter.defaultCenter().postNotificationName(APPSwithRootViewController, object: nil)
    
    }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            
            setupUI() //设置ui
            
        }
    
    private func startAnimation() {
    
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
              self.startBtn.hidden = false //隐藏按钮放在这里会对原来的数据先隐藏起来
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("ok")
        }
    }
    
        private func setupUI() {
        
            contentView.addSubview(iconImage)
            
            contentView.addSubview(startBtn)
            
            iconImage.snp_makeConstraints { (make) -> Void in  //设置图片的约束 让图片冲满整个屏幕
                make.edges.equalTo(contentView.snp_edges)
            }
            
            startBtn.snp_makeConstraints { (make) -> Void in
                make.bottom.equalTo(contentView.snp_bottom).offset(-200)
                make.centerX.equalTo(contentView.snp_centerX)
            }
            
                startBtn.hidden = true
            startBtn.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
         private lazy var iconImage: UIImageView = UIImageView()
        
        private lazy var startBtn: UIButton = UIButton(title: "现在体验", backImage: "new_feature_finish_button", color: nil)
    
//    {
//        
//           let btn = UIButton()
//            
//            btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
//            
//            btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
//            
//            btn.setTitle("现在体验", forState: .Normal)
//            
//    
//            
//            return btn
//        }()
    }
    
   

}
